package com.moyun.zbmy.zizhou.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.beans.CircleBean;
import com.moyun.zbmy.zizhou.beans.CircleVo;
import com.moyun.zbmy.zizhou.http.AbsNetCallBack;
import com.moyun.zbmy.zizhou.http.NetUtils;
import com.moyun.zbmy.zizhou.ui.activities.H5NormalActivity;
import com.moyun.zbmy.zizhou.ui.activities.ThirdCountryNewsActivity;
import com.moyun.zbmy.zizhou.ui.adapter.CommonCircleAdapter;
import com.moyun.zbmy.zizhou.ui.util.IListShowData;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 第三方区县数据
 */
public class ThirdCountryFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {
    @BindView(R.id.common_frg_refreshlayout)
    protected CanRefreshLayout mRefreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;
    private CommonCircleAdapter mAdapter;
    private boolean mIsLoading = false;

    private List<IListShowData> commonList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common_frg, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        GridLayoutManager mgr = new GridLayoutManager(getContext(), 3);
        mRecyclerView.setLayoutManager(mgr);
        mRefreshLayout.setLoadMoreEnabled(false);
//        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
//        mRecyclerView.addItemDecoration(new RBaseItemDecoration(1));
        mAdapter = new CommonCircleAdapter(getActivity(), null).setItemInternalClick(itemClick);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (isFirst) {
            mRefreshLayout.autoRefresh();
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mIsLoading = true;
        getData();
    }


    private void getData() {

        NetUtils.getNetAdapter().getCircleList(getOwnerName(), channelUrl, new AbsNetCallBack<CircleVo>(CircleVo.class) {
            @Override
            public void onSuc(CircleVo vo) {
                if (vo != null && ListUtils.isListValued(vo.getChannel())) {
                    mAdapter.setData(vo.getChannel());
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                ex.printStackTrace();
            }

            @Override
            public void onEnd() {
                onNetFinish();
            }
        });
    }


    private synchronized void onNetFinish() {
        mIsLoading = false;
        mRefreshLayout.refreshComplete();
        mRefreshLayout.loadMoreComplete();
        mRefreshLayout.setLoadMoreEnabled(false);
    }

    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            CircleBean bean = (CircleBean) mAdapter.getItem(position);
            Intent intent = new Intent(getContext(), ThirdCountryNewsActivity.class);
            intent.putExtra(ThirdCountryNewsActivity.DATA_TITLE, bean.getName());
            intent.putExtra(ThirdCountryNewsActivity.DATA_DOMAIN, bean.getDomain());
            intent.putExtra(ThirdCountryNewsActivity.DATA_URL, bean.getLink());
            startActivity(intent);
        }
    };
}
